package com.pf.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pf.auth.model.dto.ClientDetailsDto;
import com.pf.auth.service.IClientProvider;
import com.pf.base.CommonResult;
import com.pf.system.dao.SysAppInfoMapper;
import com.pf.system.model.entity.SysAppInfo;
import com.pf.system.model.entity.SysMenuInfo;
import com.pf.system.model.vo.SysAppInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName : SysUsreInfoService
 * @Description :
 * @Author : wangjie
 * @Date: 2020/9/17-10:19
 */
@Service
public class SysAppInfoService {

    @DubboReference
    private IClientProvider iClientProvider;
    @Autowired
    private SysAppInfoMapper sysAppInfoMapper;

    /**
    * @Title: 查询应用+下属菜单信息（未加权限）
    * @Param:
    * @description:
    * @author: wangjie
    * @date: 2020/11/3 21:02
    * @return:
    * @throws:
    */
    public CommonResult<List<SysAppInfoVo>> selectAppAndMenuList() {
        List<SysAppInfoVo> appInfos = sysAppInfoMapper.selectListWithMenus();
        List<ClientDetailsDto> detailsDtos = iClientProvider.list();
        appInfos.forEach(e -> {
            e.setAppActiveRule("/".concat(e.getAppId()));
            if(!CollectionUtils.isEmpty(e.getSysMenuInfos())) {
                List<SysMenuInfo> roots = new ArrayList<>();
                Iterator<SysMenuInfo> it = e.getSysMenuInfos().iterator();
                while (it.hasNext()) {
                    SysMenuInfo menuInfo = it.next();
                    if(menuInfo.getMenuId().equals(menuInfo.getMenuSupMenuId())) {
                        roots.add(menuInfo);
                        it.remove();
                    }
                }
                for (SysMenuInfo child : e.getSysMenuInfos()) {
                    child.addChildren(roots);
                }
                e.setSysMenuInfos(roots);
            }
            // 赋值oauth属性
            Optional<ClientDetailsDto> optional = detailsDtos.stream().filter(clientDetailsDto -> clientDetailsDto.getClientId().equals(e.getAppId())).findFirst();
            optional.ifPresent(clientDetailsDto -> {
                e.setClientDetailsDto(clientDetailsDto);
            });
        });
        return CommonResult.success(appInfos);
    }

    public CommonResult<List<SysAppInfoVo>> selectAppList() {
        List<SysAppInfo> list = sysAppInfoMapper.selectList(Wrappers.emptyWrapper());
        List<ClientDetailsDto> detailsDtos = iClientProvider.list();
        List<SysAppInfoVo> vos = new ArrayList<>();
        // 赋值oauth属性
        for (SysAppInfo sysAppInfo : list) {
            SysAppInfoVo vo = SysAppInfoVo.buildBy(sysAppInfo, SysAppInfoVo.class);
            Optional<ClientDetailsDto> optional = detailsDtos.stream().filter(clientDetailsDto -> clientDetailsDto.getClientId().equals(sysAppInfo.getAppId())).findFirst();
            optional.ifPresent(clientDetailsDto -> {
                vo.setClientDetailsDto(clientDetailsDto);
            });
            vos.add(vo);
        }
        return CommonResult.success(vos);
    }

    // TODO 分布式事务问题
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<String> addApp(SysAppInfoVo sysAppInfo) {
        sysAppInfoMapper.insert(SysAppInfo.buildByVo(sysAppInfo, SysAppInfo.class));
        iClientProvider.add(sysAppInfo.getClientDetailsDto());
        return CommonResult.success();
    }

}
